//index.ts
import { createAlova } from 'alova'
import GlobalFetch from 'alova/GlobalFetch'
import VueHook from 'alova/vue'

export const AlovaInstance = createAlova({
  baseURL: 'http://www.cosck.com:444/comic/sever/',
  statesHook: VueHook,
  requestAdapter: GlobalFetch(),
  //shareRequest: true,

  // 请求拦截器
  beforeRequest() {},
  // 响应拦截器
  responded: {
    onSuccess: async (response: Response) => {
      const json = await response.json()      
      // 根据后端约定，判断code值抛出对应的问题
      if (json.code === '1000'||json.code === 1000) {
        return json.data
      } else {
        console.log(json.msg)
        throw new Error(json.msg)
      }
    },
    // 抛出错误时，将会进入请求失败拦截器内
    onError(error) {
      throw new Error(error)
    }
  }
})